/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package hilos;

import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.OutputStreamWriter;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.swing.JTextArea;

/**
 *
 * @author Felipe Serrano
 */
public class HiloMostrarSalida extends Thread {

    private JTextArea text;
    private Process p;
    private String file;

    public HiloMostrarSalida(JTextArea text,Process p,String file){
        this.text= text;
        this.p = p;
        this.file = file;

    }

    public void run() {
        String line;
        try {
            FileOutputStream os = new FileOutputStream(file);
            BufferedWriter salida = new BufferedWriter(new OutputStreamWriter(os));
            BufferedReader is = new BufferedReader(new InputStreamReader(p.getInputStream()));
            while ((line = is.readLine()) != null){
                this.text.append(line+"\n");
                this.text.setCaretPosition(text.getText().length());
                salida.write(line+"\n");
            }
            salida.close();
        } catch (IOException ex) {
            Logger.getLogger(HiloMostrarSalida.class.getName()).log(Level.SEVERE, null, ex);
        }


    }
}
